package com.stgy.lease.web.admin.controller.apartment;

import com.mysql.cj.log.Log;
import com.stgy.lease.common.redis.RedisPrefixEnum;
import com.stgy.lease.common.result.Result;
import com.stgy.lease.model.domain.CityInfo;
import com.stgy.lease.model.domain.DistrictInfo;
import com.stgy.lease.model.domain.ProvinceInfo;
import com.stgy.lease.web.admin.service.CityInfoService;
import com.stgy.lease.web.admin.service.DistrictInfoService;
import com.stgy.lease.web.admin.service.ProvinceInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * ClassName:RegionController
 * Package:com.stgy.lease.web.admin.controller
 * Description:
 *
 * @Author:helei
 * @Create:2024/8/31 - 8:40
 */
@RestController
@RequestMapping("/admin/region")
@Tag(name = "地区信息管理模块")
public class RegionController {

    @Autowired
    private ProvinceInfoService provinceInfoService;

    @Autowired
    private CityInfoService cityInfoService;

    @Autowired
    private DistrictInfoService districtInfoService;

    @Autowired
    private RedisTemplate redisTemplate;

    private String RegionKey = RedisPrefixEnum.STGY_ADMIN_PREFIX.getPrefix();

    //    http://localhost:5174/admin/region/province/list GET
    @Operation(summary = "查询省份信息列表")
    @GetMapping("/province/list")
    public Result<List<ProvinceInfo>> provinceList() {
        List<ProvinceInfo> list = (List<ProvinceInfo>) redisTemplate.opsForValue().get(RegionKey + "province");
        if (list == null) {
            list = provinceInfoService.list();
            redisTemplate.opsForValue().set(RegionKey + "province", list, 1, TimeUnit.DAYS);
        }
        return Result.ok(list);
    }

    //http://localhost:5174/admin/region/city/listByProvinceId?id=11 GET
    @Operation(summary = "根据省份id查询城市信息列表")
    @GetMapping("/city/listByProvinceId")
    public Result<List<CityInfo>> listByProvinceId(Long id) {
        List<CityInfo> list = (List<CityInfo>) redisTemplate.opsForValue().get(RegionKey + "city:"+id);
        if (list == null) {
            list = cityInfoService.listByProvinceId(id);
            redisTemplate.opsForValue().set(RegionKey + "city:"+id, list, 1, TimeUnit.DAYS);
        }
        return Result.ok(list);
//        return Result.ok(cityInfoService.listByProvinceId(id));
    }

    //http://localhost:5174/admin/region/district/listByCityId?id=1301 GET
    @Operation(summary = "根据城市id查询区县信息")
    @GetMapping("/district/listByCityId")
    public Result<List<DistrictInfo>> listByCityId(Long id) {
        List<DistrictInfo> list = (List<DistrictInfo>) redisTemplate.opsForValue().get(RegionKey + "district:"+id);
        if (list == null) {
            list = districtInfoService.listByCityId(id);
            redisTemplate.opsForValue().set(RegionKey + "district:"+id, list, 1, TimeUnit.DAYS);
        }
        return Result.ok(list);
//        return Result.ok(districtInfoService.listByCityId(id));
    }
}
